package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Integral;
import cn.wolfcode.trip.base.domain.IntegralChange;
import cn.wolfcode.trip.base.domain.OrderList;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.IntegralChangeMapper;
import cn.wolfcode.trip.base.mapper.IntegralMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IIntegralService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IIntegralServiceImpl implements IIntegralService {
    @Autowired
    private IntegralMapper integralMapper;
    @Autowired
    private IntegralChangeMapper changeMapper;
    @Override
    public void sign(Sign sign) {
        if(sign==null){
            return;
        }
        //获取变动的参数
        IntegralChange change = IntegralChange.getObject();
        //设置积分变动关联对象
        change.setUser(sign.getUser());
        change.setChangeTime(new Date());
        if (sign.getSigncount()>1){
            //如果开始连续签到,每次签到积分+5
            change.setChangeNum(sign.getSigncount()*5);
            //签到次数为3时赠送小礼品
        }
        //把变动记录插入到插入数据
        changeMapper.insert(change);
        //积分表修改数据
        /*
         * 1.先根据id获取到积分对象
         * 2.如果没有对象说明是第一次签到直接插入数据
         * 3.如果有对象,从对象获得原有的积分
         * 4.再根据积分变动类型,执行积分扣减和增加操作,
         *  ps这是登录操作也没有必要判断减少..
         * 5.修改数据库中的数据
         */
        Integral integral = integralMapper.selectByPrimaryKey(sign.getUser().getId());
        if (integral == null) {
            integral = new Integral();
            integral.setUser_id(change.getUser().getId());
            integral.setCount(change.getChangeNum());
            integralMapper.insert(integral);
            return;
        }
        if(change.getChangeType()==IntegralChange.TYPE_SAVE){
            integral.setCount(integral.getCount()+change.getChangeNum());
            integralMapper.updateByPrimaryKey(integral);
        }
    }

    @Override
    public Integral getIntegralByuserId(Long userId) {
        return integralMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void shop(OrderList orderList) {
        //积分记录对象
        IntegralChange integralChange = new IntegralChange();
        integralChange.setChangeTime(new Date());
        integralChange.setChangeNum(orderList.getPrice());
        integralChange.setUser(orderList.getUser());
        //类型为减少
        integralChange.setIntegral(1);
        //积分途径
        integralChange.setChangeType(1);
        //插入记录
        changeMapper.insert(integralChange);
        //扣减积分
        Integral integral = integralMapper.selectByPrimaryKey(orderList.getUser().getId());
        integral.setCount(integral.getCount()-orderList.getPrice());
        integralMapper.updateByPrimaryKey(integral);
    }

    @Override
    public PageInfo query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize(), queryObject.getOrderBy());
        List<IntegralChange> list = changeMapper.selectForList(queryObject);
        return new PageInfo(list);
    }
}
